package com.fronald.getbot.misc;

import com.fronald.interfaces.LocalConfiguration;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.fronald.libjutil.FileUtil;

/**
 *
 * @author franklin
 */
public class Configuracao implements LocalConfiguration {

    private String proxyServer = "";
    private int proxyPort = 0;
    private String proxyUsername = "";
    private String proxyPassword = "";
    private int conTimeout = 0;
    private int sockTimeout = 0;
    private boolean showLoader = false;
    private boolean selPlugins = false;
    private boolean useProxy = false;
    private String decimalSeparator = ".";
    private String lastFolder = "";
    private int attempts = 0;
    File arquivo = null;
    private static String fileName = ".getBot";

    public void carregaConfiguracao() throws FileNotFoundException, IOException {
        carregaArquivo();
        Properties p = new Properties();
        p.load(new FileInputStream(arquivo));
        this.proxyServer = p.getProperty("proxyServer");
        this.proxyPort = Integer.parseInt(p.getProperty("proxyPort"));
        this.proxyUsername = p.getProperty("proxyUsername");
        this.proxyPassword = p.getProperty("proxyPassword");
        this.useProxy = Boolean.parseBoolean(p.getProperty("useProxy"));
        this.conTimeout = Integer.parseInt(p.getProperty("conTimeout"));
        this.sockTimeout = Integer.parseInt(p.getProperty("sockTimeout"));
        this.showLoader = Boolean.parseBoolean(p.getProperty("showLoader"));
        this.selPlugins = Boolean.parseBoolean(p.getProperty("selPlugins"));
        this.decimalSeparator = p.getProperty("decimalSeparator");
        this.attempts = Integer.parseInt(p.getProperty("attempts"));
        this.lastFolder = p.getProperty("lastFolder");
    }

    public void salvaConfiguracao() throws IOException {
        carregaArquivo();
        Properties p = new Properties();
        p.setProperty("decimalSeparator", this.decimalSeparator);
        p.setProperty("proxyServer", this.proxyServer);
        p.setProperty("proxyPassword", this.proxyPassword);
        p.setProperty("proxyUsername", this.proxyUsername);
        p.setProperty("proxyPort", Integer.toString(this.proxyPort));
        p.setProperty("conTimeout", Integer.toString(this.conTimeout));
        p.setProperty("sockTimeout", Integer.toString(this.sockTimeout));
        p.setProperty("selPlugins", Boolean.toString(this.selPlugins));
        p.setProperty("showLoader", Boolean.toString(this.showLoader));
        p.setProperty("useProxy", Boolean.toString(this.isUseProxy()));
        p.setProperty("attempts", Integer.toString(this.attempts));
        p.setProperty("lastFolder", this.lastFolder);
        this.arquivo.delete();
        this.arquivo.createNewFile();
        p.store(new FileOutputStream(this.arquivo), "Configuracao do GetBot");
    }

    public String getProxyServer() {
        return proxyServer;
    }

    public void setProxyServer(String proxyServer) {
        this.proxyServer = proxyServer;
    }

    public int getProxyPort() {
        return proxyPort;
    }

    public void setProxyPort(int proxyPort) {
        this.proxyPort = proxyPort;
    }

    public String getProxyUsername() {
        return proxyUsername;
    }

    public void setProxyUsername(String proxyUsername) {
        this.proxyUsername = proxyUsername;
    }

    public String getProxyPassword() {
        return proxyPassword;
    }

    public void setProxyPassword(String proxyPassword) {
        this.proxyPassword = proxyPassword;
    }

    public int getConTimeout() {
        return conTimeout;
    }

    public void setConTimeout(int conTimeout) {
        this.conTimeout = conTimeout;
    }

    public int getSockTimeout() {
        return sockTimeout;
    }

    public void setSockTimeout(int sockTimeout) {
        this.sockTimeout = sockTimeout;
    }

    public boolean isShowLoader() {
        return showLoader;
    }

    public void setShowLoader(boolean showLoader) {
        this.showLoader = showLoader;
    }

    public boolean isSelPlugins() {
        return selPlugins;
    }

    public void setSelPlugins(boolean selPlugins) {
        this.selPlugins = selPlugins;
    }

    public String getDecimalSeparator() {
        return decimalSeparator;
    }

    public void setDecimalSeparator(String decimalSeparator) {
        this.decimalSeparator = decimalSeparator;
    }

    private void carregaArquivo() throws IOException {
        if (this.arquivo == null) {
            String dirUsuario = System.getProperty("user.home");
            File cfg = new File(dirUsuario + "/" + fileName);
            if (!cfg.exists()) {
                FileUtil.saveStringOnFile(cfg.getAbsolutePath(), "lastFolder=\nattempts=50\nproxyServer=192.168.10.1\nproxyPort=3128\nproxyUsername=usuario\nproxyPassword=senha\nuseProxy=false\nconTimeout=2000\nsockTimeout=2000\nshowLoader=false\nselPlugins=false\ndecimalSeparator=,\n");
            }
            this.arquivo = cfg;
        }
    }

    public boolean isUseProxy() {
        return useProxy;
    }

    public void setUseProxy(boolean useProxy) {
        this.useProxy = useProxy;
    }

    public int getAttempts() {
        return attempts;
    }

    public void setAttempts(int attemps) {
        this.attempts = attemps;
    }

    public String getLastFolder() {
        return lastFolder;
    }

    public void setLastFolder(String lastFolder) {
        this.lastFolder = lastFolder;
    }
}
